﻿using Dal;
using IDal;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace BLL
{
    public abstract class BaseService<T> where T : class, new()
    {
        public IBaseDal<T> CurrentDal;//依赖抽象接口 

        /// <summary>
        /// 构造函数
        /// </summary>
        protected BaseService()
        {
            //执行给当前的 CurrentDal 赋值
            //强迫子类给当前类的 CurrentDal 属性赋值
            SetCurrentDal();
        }

        public abstract void SetCurrentDal();

        public async Task<T> AddAsync(T entity)
        {
            T t1 = await CurrentDal.AddAsync(entity);
            return t1;
        }

        public Task<T> UpdateAsync(T entity)
        {
            return CurrentDal.UpdateAsync(entity); ;
        }

        public Task<bool> DeleteAsync(T entity)
        {

            return CurrentDal.DeleteAsync(entity); ;
        }

        public Task<T> GetModelAsync(Expression<Func<T, bool>> whereLambda)
        {
            return CurrentDal.GetModelAsync(whereLambda);
        }
        public Task<IList<T>> GetListAsync(Expression<Func<T, bool>> whereLambda)
        {
            return CurrentDal.GetListAsync(whereLambda);
        }

        public Task<Tuple<IList<T>, int>> GetPageAsync<TS>(int pageSize, int pageIndex, Expression<Func<T, bool>> whereLambda, Expression<Func<T, TS>> orderByLambda, bool isAsc)
        {
            return CurrentDal.GetPageAsync(pageSize, pageIndex, whereLambda, orderByLambda, isAsc);
        }
    }
}
